#include "widget.h"
#include "ui_widget.h"
#include<QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //创建行
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);

    //创建列
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);


    //设置列名
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("年龄"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("学号"));

    //添加数据
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("1001"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("20"));

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("1002"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("30"));

    ui->tableWidget->setItem(2,0,new QTableWidgetItem("1003"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("王五"));
    ui->tableWidget->setItem(2,2,new QTableWidgetItem("40"));

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_row_clicked()
{
    //获取当前行数
    int row = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(row);
    qDebug()<<row;

    ui->pushButton_row->setShortcut(QKeySequence("="));

}

void Widget::on_pushButton_col_clicked()
{
        //获取列
    int col =ui->tableWidget->columnCount();
    ui->tableWidget->insertColumn(col);

    //设置列名
    const QString&text = ui->lineEdit->text();
     ui->tableWidget->setHorizontalHeaderItem(col,new QTableWidgetItem(text));
}
void Widget::on_pushButton_delrow_clicked()
{
        //删除行
    int row =ui->tableWidget->rowCount();
    ui->tableWidget->removeRow(row);



}

void Widget::on_pushButton_delcol_clicked()
{
     int col =ui->tableWidget->columnCount();
     ui->tableWidget->removeColumn(col);

}
